/**
 * @author spider
 * This class saves the traffic class contends on a xml file
 */
package br.unb.plugins.geral.model;

import java.net.InetAddress;
import java.net.UnknownHostException;

import br.unb.core.traffic.Traffic;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class XmlTrafficConverter implements Converter {

    @Override
    public boolean canConvert(Class unknowclass) {
        return unknowclass.equals(Traffic.class);
    }

    @Override
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        Traffic t = (Traffic) source;

        writer.addAttribute("name", t.getName());
        writer.addAttribute("srcIP", t.getSrcIP().getHostAddress());
        writer.addAttribute("srcPort", Integer.toString(t.getSrcPort()));
        writer.addAttribute("dstIP", t.getDstIP().getHostAddress());
        writer.addAttribute("dstPort", Integer.toString(t.getDstPort()));
        writer.addAttribute("start", Integer.toString(t.getStart()));
        writer.addAttribute("duration", Integer.toString(t.getDuration()));
        writer.addAttribute("tos", t.getTos());
        writer.addAttribute("pauseType", t.getPauseType());
        writer.addAttribute("lengthType", t.getLengthType());
        if (t.getPauseModel() != null) {
            writer.startNode("pauseModel");
            context.convertAnother(t.getPauseModel());
            writer.endNode();
        }
        if (t.getLengthModel() != null) {
            writer.startNode("lenghtModel");
            context.convertAnother(t.getLengthModel());
            writer.endNode();
        }

        writer.startNode("path");
        if (t.getPath() != null) {
            writer.setValue(t.getPath());
        } else {
            writer.setValue("none");
        }
        writer.endNode();

    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        InetAddress address = null;
        String s = null;
        Integer i = 0;

        Traffic traffic = new Traffic();
        s = reader.getAttribute("name");
        traffic.setName(s);

        try {
            address = InetAddress.getByName(reader.getAttribute("srcIP"));
        } catch (UnknownHostException ex) {
            address = null;
        }
        traffic.setSrcIP(address);

        s = reader.getAttribute("srcPort");
        i = Integer.parseInt(s);
        traffic.setSrcPort(i);

        try {
            address = InetAddress.getByName(reader.getAttribute("dstIP"));
        } catch (UnknownHostException ex) {
            address = null;
        }
        traffic.setDstIP(address);

        s = reader.getAttribute("dstPort");
        i = Integer.parseInt(s);
        traffic.setDstPort(i);

        s = reader.getAttribute("start");
        i = Integer.parseInt(s);
        traffic.setStart(i);

        s = reader.getAttribute("duration");
        i = Integer.parseInt(s);
        traffic.setDuration(i);

        s = reader.getAttribute("tos");
        traffic.setTos(s);

        s = reader.getAttribute("pauseType");
        traffic.setPauseType(s);

        s = reader.getAttribute("lengthType");
        traffic.setLengthType(s);

        while (reader.hasMoreChildren()) {
            reader.moveDown();
            s = reader.getNodeName();
            if (s.equals("pauseModel")) {
                GeneratorModel pm = (GeneratorModel) context.convertAnother(traffic, GeneratorModel.class);
                traffic.setPauseModel(pm);
            } else if (s.equals("lenghtModel")) {
                GeneratorModel lm = (GeneratorModel) context.convertAnother(traffic, GeneratorModel.class);
                traffic.setLengthModel(lm);
            } else if (s.equals("path")) {
                traffic.setPath(reader.getValue());
            }
            reader.moveUp();
        }
        return traffic;
    }
}